package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import lombok.Data;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.Formatter;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @program: cms-vue-plus
 * @description: 凤凰网军事
 * @author: gaoX
 * @create: 2021-08-24 13:42
 **/
@TargetUrl(value = "https://(mil.)?(ent.)?(tech.)?(finance.)?(sports.)?ifeng.com/c/\\S{5,11}")
@Data
public class IFengNews implements AfterExtractor {

	@ExtractBy(value = "//div[@class='artical-25JfwmD5']//h1[@class='topic-z1ycr_72']//text()", notNull = true)
	private String title;

	@ExtractBy(value = "//div[@class='main_content-r5RGqegj']//p/text()", notNull = true)
	private List<String> content;

	@ExtractBy("//div[@class='main_content-r5RGqegj']//p//img/@src")
	private List<String> imgList;

	@Formatter(formatter = DataTemplateFormatter.class)
	@ExtractBy("//p[@class='time-1Mgp9W-1']//span[1]/text()")
	private Date date;

	@ExtractBy("//div[@class='breadcrumbs-MEZ5W2XH']/span[1]/a/text()")
	private String category;


	@Override
	public void afterProcess(Page page) {
		ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
		SpiderNews spider = new SpiderNews();
		spider.setNewsTitle(ChinaNews.filterSpecialCharacter(title));
		StringBuffer sb = new StringBuffer();
		content.forEach(s -> sb.append(s+"$$$"));
		String content = ChinaNews.filterSpecialCharacter(sb.toString());
		spider.setNewsContent(StrUtil.isEmpty(content)?"":content);
		spider.setContentSize(spider.getNewsContent().length());

		Iterator<String> iterator = imgList.iterator();
		while(iterator.hasNext()){
			if(iterator.next().contains("gif")){
				iterator.remove();
			}
		}
//		spider.setNewsPicJson(JSONUtil.toJsonStr(imgList));
		spider.setNewsUrl(page.getUrl().toString());
		spider.setNewsPublicTime(date);
		spider.setUpdateBy("admin");
		spider.setCreateTime(DateUtil.date());
		spider.setNewsSource("ifeng.com");
		if(category.contains("军事")){
			spider.setNewsCategory("凤凰网军事");
		}else if(category.contains("娱乐")){
			if(title.contains("《")){
				return;
			}
			spider.setNewsCategory("凤凰网娱乐");
		}else if(category.contains("科技")){
			int i = RandomUtil.randomInt(1, 4);
			spider.setNewsCategory("凤凰网科技-"+i);
		}else if(category.contains("财经")){
			spider.setNewsCategory("凤凰网财经");
		}else {
			return;
		}

		if(sb.length()<5000&&sb.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length()>20&&imgList.size()<20){
			RedisService redisService = SpringUtils.getBean(RedisService.class);
			Date ago = DateUtil.offsetDay(new Date(),-3);
			if(ObjectUtil.isNull(date)){
				date = new Date();
			}
			int i = date.compareTo(ago);
			//redis查询是否存在
			boolean ismember = redisService.isMember(SecureUtil.md5(title));
			if(!ismember&&i>0){//如果不存在插入mysql和redis
				List<String> resImg = DataSaveUtil.delPic(imgList);
				spider.setNewsPicJson(JSONUtil.toJsonStr(resImg));
				service.save(spider);
				redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date,title);
			}
		}
	}
}
